package com.serita.demo.adapter;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.serita.demo.QQActivity;
import com.serita.demo.R;

import java.util.List;

/**
 * Created by Administrator on 2018/2/26 0026.
 */

public class MinePrizeResultAdapter extends BaseExpandableListAdapter {
    private final QQActivity context;
    private final List<List<String>> childrenData;
    private final List<String> groupData;

    public MinePrizeResultAdapter(QQActivity qqActivity, List<List<String>> childrenData, List<String> groupData) {
        this.context = qqActivity;
        this.childrenData=childrenData;
        this.groupData=groupData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childrenData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//        TextView text = null;
        if (convertView != null) {
//            text = (TextView)convertView;
//            text.setText(childrenData.get(groupPosition).get(childPosition));
            TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
            ImageView headView = (ImageView) convertView.findViewById(R.id.iv_icon);
            tvName.setText(childrenData.get(groupPosition).get(childPosition));
        } else {
//            text = createView(childrenData.get(groupPosition).get(childPosition));

//            View view = View.inflate(context, R.layout.item_prize_detail, null);
//            TextView tvName = (TextView) view.findViewById(R.id.tv_name);
//            tvName.setText("陈立");
            convertView = View.inflate(context, R.layout.item_prize_detail, null);
            TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
            ImageView headView = (ImageView) convertView.findViewById(R.id.iv_icon);
            tvName.setText(childrenData.get(groupPosition).get(childPosition));

        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childrenData.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupData.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groupData.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//        TextView text = null;
        if (convertView != null) {
//            text = (TextView)convertView;
//            text.setText(groupData.get(groupPosition));
            TextView name = (TextView) convertView.findViewById(R.id.tv_title);
            name.setText("      奖品："+groupData.get(groupPosition));
        } else {
//            text = createView(groupData.get(groupPosition));

//            View view = View.inflate(context, R.layout.item_prize_title, null);
//            TextView name = (TextView) view.findViewById(R.id.tv_title);
//            name.setText("        奖品：groupData.get(groupPosition)");

            convertView = View.inflate(context, R.layout.item_prize_title, null);
            TextView name = (TextView) convertView.findViewById(R.id.tv_title);
            name.setText("      奖品："+groupData.get(groupPosition));
        }
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private TextView createView(String content) {
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, 150);
        TextView text = new TextView(context);
        text.setLayoutParams(layoutParams);
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        text.setPadding(40, 0, 0, 0);
        text.setText(content);

        return text;
    }

    private void showMessage(String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }
}
